/****************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "DownloadSaveWindow.h"
#include "../../Network/networkops.h"
#include "../../Controls/Application.h"
#include "../../FileOperations/fileops.h"
#include "../../FileOperations/DirList.h"
#include "ManageConnectTask.hpp"
#include "DownloadWiiSaveTask.hpp"

DownloadSaveWindow::DownloadSaveWindow(u64 Tid, std::string ID, GuiFrame * parentElement)
	: GuiFrame(572, 372, parentElement)
	, tid(Tid)
	, id(ID)
	, selectedSave(-1)
	, dlFilepath("")
	, dlFilename("")
	, saveList(NULL)
	, saveBrowser(NULL)
{
	SetAlignment(ALIGN_CENTER | ALIGN_MIDDLE);
	Result.connect(this, &DownloadSaveWindow::OnResult);
	
	connectButtons = new ManageButtons(tr("No network connection"),
									   tr("Do you want to connect?"),
									   tr("Yes"),
									   tr("Cancel"));
	connectButtons->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	connectButtons->SetPosition(-6, -30);
	connectButtons->SetEffect(EFFECT_FADE, 20);
	connectButtons->ButtonClicked.connect(this, &DownloadSaveWindow::OnConnectClick);
	
	pathButtons = new ManageButtons(tr("Select a target path."),
									tr("Be carrefull, present files can be overwritten"),
									tr("OK"),
									tr("Cancel"));
	pathButtons->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	pathButtons->SetPosition(-6, -30);
	pathButtons->SetEffect(EFFECT_FADE, 20);
	pathButtons->ButtonClicked.connect(this, &DownloadSaveWindow::OnPathClick);
	
	resultButtons = new ManageButtons(0,
									  0,
									  tr("OK"));
	resultButtons->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	resultButtons->SetPosition(-6, -30);
	resultButtons->SetEffect(EFFECT_FADE, 20);
	resultButtons->ButtonClicked.connect(this, &DownloadSaveWindow::OnResultClick);
	
	if(id.empty())
	{
		Result(-1);
		return;
	}
	
	if(!IsNetworkInit())
		Append(connectButtons);
	else
	{
		GetSaveListTask * saveListTask = new GetSaveListTask(id);
		saveListTask->TaskEnd.connect(this, &DownloadSaveWindow::OnGetSaveListEnd);
	}
}

DownloadSaveWindow::~DownloadSaveWindow()
{
	parent->Remove(this);
	
	RemoveAll();
	
	delete connectButtons;
	delete pathButtons;
	delete resultButtons;
	
	if(saveBrowser)
		delete saveBrowser;
	if(saveList)
		delete saveList;
}

void DownloadSaveWindow::OnConnectClick(ManageButtons * connect UNUSED, int choice)
{
	Remove(connectButtons);
	
	if(choice == 1)
	{
		ManageConnectTask * connectTask = new ManageConnectTask();
		connectTask->TaskEnd.connect(this, &DownloadSaveWindow::OnConnectTaskEnd);
	}
	else
	{
		//! cancel
		Result(0);
	}
}
	
void DownloadSaveWindow::OnConnectTaskEnd(int result)
{
	if(result == 0)
		Result(0);
	else if(result == -1)
		Result(-2);
	else
	{
		GetSaveListTask * saveListTask = new GetSaveListTask(id);
		saveListTask->TaskEnd.connect(this, &DownloadSaveWindow::OnGetSaveListEnd);
	}
}

void DownloadSaveWindow::OnGetSaveListEnd(WiiSave_List * list , int result)
{
	if(result < 0)
	{
		Result(result);
		return;
	}
	
	saveList = list;
	
	saveBrowser = new WiiSaveBrowser(saveList, this);
	saveBrowser->Closed.connect(this, &DownloadSaveWindow::OnSaveBrowserClose);
}

void DownloadSaveWindow::OnSaveBrowserClose(int choice)
{
	while(saveBrowser->IsAnimated())
		Application::Instance()->updateEvents();
	delete saveBrowser;
	saveBrowser = NULL;
	
	if(choice < 0)
	{
		Result(0);
		return;
	}
	
	selectedSave = choice;
	
	Append(pathButtons);
}

void DownloadSaveWindow::OnPathClick(ManageButtons * path UNUSED, int choice)
{
	Remove(pathButtons);
	
	if(choice == 1)
	{
		PathBrowser *pathBrowser = new PathBrowser(Settings.DataBinPath);
		pathBrowser->ButtonClick.connect(this, &DownloadSaveWindow::OnPathBrowserButtonClick);
	}
	else
	{
		//! cancel
		Result(0);
	}
}

void DownloadSaveWindow::OnPathBrowserButtonClick(PathBrowser *menu, u8 filter UNUSED, bool doneClicked, const std::string &Path)
{
	menu->SetEffect(EFFECT_FADE, -20);
	while(menu->IsAnimated())
		Application::Instance()->updateEvents();
	menu->Close();
	
	if(!doneClicked)
	{
		Result(0);
		return;
	}
	
	//! download file
	DownloadWiiSaveTask * dlTask = new DownloadWiiSaveTask(Path, tid, id, saveList, selectedSave);
	dlTask->TaskEnd.connect(this, &DownloadSaveWindow::OnDownloadTaskEnd);
}

void DownloadSaveWindow::OnDownloadTaskEnd(int result)
{
	delete saveList;
	saveList = NULL;
	
	Result(result);
}

void DownloadSaveWindow::OnResult(int result)
{
	if(result > 0)
		resultButtons->SetTitle(tr( "Succesfully downloaded" ));
	else if(result == 0)
		resultButtons->SetTitle(tr( "Download cancelled" ));
	else if(result < 0)
	{
		if (result == -1)
			resultButtons->SetTitle(tr("Bad game ID"));
		else if (result == -2)
			resultButtons->SetTitle(tr("Could not initialize network!"));
		else if (result == -3)
			resultButtons->SetTitle(tr("Connection to server timed out."));
		else if (result == -4)
			resultButtons->SetTitle(tr("No saves found on the site."));
		else if (result == -5)
			resultButtons->SetTitle(tr("No saves available for this region."));
		else if (result == -6)
			resultButtons->SetTitle(tr("Unable to download file."));
		else if (result == -7)
			resultButtons->SetTitle(tr("Unable to extract files."));
		else if (result == -8)
			resultButtons->SetTitle(tr("Unable to find supported files."));
		else if (result == -9)
			resultButtons->SetTitle(tr("Unable to write files."));
	}
	
	Append(resultButtons);
}

void DownloadSaveWindow::OnResultClick(ManageButtons * result UNUSED, int choice UNUSED)
{
	Remove(resultButtons);
	
	End();
}
